﻿using System;
using System.Globalization;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class AddressRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>, IAddressRepository<TItem>
        where TItem : Interfaces.IAddress, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, new()
    {
        public AddressRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public override TItem Get(Guid nodeId)
        {
            return base.Get(nodeId, CultureInfo.InvariantCulture, ContentState.Draft);
        }

        public TItem GetDefaultAddress(Guid parentNodeId)
        {
            return ChildsOf(parentNodeId, false, 0,CultureInfo.InvariantCulture,ContentState.Draft,ActiveStatus.All)
                .Where(a => a.IsDefault.GetValueOrDefault(false))
                .FirstOrDefault();
        }
    }

    public class AddressRepository : AddressRepository<AddressCollection, Address>, IAddressRepository
    {
        public AddressRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper) : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }

    public interface IAddressRepository<T> : IMedianamikRepository<T>
        where T : Interfaces.IAddress
    {
        T GetDefaultAddress(Guid nodeId);
    }

    public interface IAddressRepository: IAddressRepository<Address>
    {
    }

}
